$(function(){
	//初始化
	init();
	bindFun();
});
var contractId ;
var policyRef;
var result; //NEWTOUCH数据
var da;
var dataList;
function init(){
	contractId=sessionStorage.contractId;
	policyRef=sessionStorage.policyRef;
	//初始化NEWTOUCH数据
	padMarketing.ajaxNormal(
			"/pad_marketing/note/initMyContent.action",
			{"contractId" :contractId},
			function(data){
				result = data.result;
				if(result.changeWay){
					handleData(result);
				}
				//初始化Oliver数据
				padMarketing.ajaxNormal(
						"/pad_marketing/note/initContent.action",
						{"contractId" :contractId},
						function(data){
							da=data;
							dataList = data.dataList;
							showLoadingPad("加载中");
						});
			}
	);
	$("#imgPdf").attr("src","/pad_marketing/note/showPdfPic.action?contractId="+contractId+"&flag=0"+"&type=NL4003"+"&policyRef="+policyRef).error(function(){
		alertMsg("网速较差,未生成pdf,请更换网络",0,function(){
			location.href ="noteContent.action";
		});
	});
	
	$("#imgPdf").attr("src","/pad_marketing/note/showPdfPic.action?contractId="+contractId+"&flag=0"+"&type=NL4003"+"&policyRef="+policyRef).load(function(){
		if(myScroll)
			myScroll.refresh();
		hideLoadingPad();
		//初始化签名配置
		setTimeout(function(){
			initSign(policyRef,'3');
		}, 2000);
		
	});
}
function bindFun(){
	$("#next").click(function(){
		if(result.sign=="Y"){
			if(dataList.length!=0){
				location.href="/pad_marketing/note/noteIssue.action?url=quotationSign.action&policyRef="+policyRef+"&contractId="+contractId;
				return;
			}else{
				location.href="/pad_marketing/note/affirmNoteReply.action?url=quotationSign.action&policyRef="+policyRef+"&contractId="+contractId;
				return;
			}
		}
		//这里应该是签名
		//投保单--投保人签名
		var sign1 = $("#sign1").attr("style");
		if('display: none;'!=sign1 ){
			alertMsg('请完成保价信（投保人）签名！');
			return;
		}
		//校验投保单--投保人拍照
		if(''==src3){
			alertMsg('请拍摄投保人胸部以上清晰的正面照！');
			return;
		}
		//调壳生成加密字符串
		finishSign("doSign");
		
	
	});
}

//处理数据
function handleData(result){
	if(result.sign=="Y"){
		$("#policySign").html('<img src="../html/img/signIdY.png" width="98" >');
	}
	var flag = result.changeWay;
	if(flag=="one"){
		$("#one").attr("class","radio_checked");
	}else if (flag=="two"){
		$("#two").attr("class","radio_checked");
	}else if(flag=="otherWenben"){
		$("#otherWenben").attr("class","radio_checked");
		$("#wenben").val(result.wenben);
	}
	$("#wenben").attr("disabled","disabled");
	$("#one").off("click");
	$("#two").off("click");
	$("#otherWenben").off("click");
	
}
// 壳回调本地方法  展示签字     src是数据  pdfNum是pdf界面  siger是0 imgNum是img
function showSign(src,pdfNum,siger,imgNum){
	var num = imgNum.substr(3,1);
	$("#sign"+num).hide();
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}
//拍照回调
var src3 = "";
function showPhoto(src,imgNum){
	if('pho1'==imgNum)src3 = src;
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;			
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}
//pdf是加密包
function doSign(pdf4){
	var signKey=contractId+"NEWTOUCH";
	$.get('/pad_marketing/main/getSign.action',{'jsonParam':JSON.stringify({signKey:signKey})},function(data){
		
		var pdfURL1 = data.address+"NL4003?contractId="+contractId+"&partner=NEWTOUCH&sign="+data.sign;
		var photoList1 = [src3];
		var pdfMessageList = [];
		if(pdfURL1){
			pdfMessageList.push({"pdfType":"NL4003","pdfURL":pdfURL1,"encode":pdf4,"photoList":photoList1});
		}
		var params={"policyRef":policyRef,"pdfMessageList":pdfMessageList,"insured":contractId};
		
		padMarketing.ajaxNormal('/pad_marketing/note/QCBodySignCA.action',params,
				function(data){
				var changeWay =$("span[class='radio_checked']").attr("id");
			var data={};
			if(changeWay=="otherWenben"){
				var wenben = $("#wenben").val();
				result.changeWay = changeWay;
				result.wenben = wenben;
				
			}else{
				result.changeWay = changeWay;
			}
			
			
			
			//记录操作
			result.sign="Y";
			padMarketing.ajaxNormal(
					"/pad_marketing/note/updateMyContent.action",
					result,
					function(data){
						if(dataList.length!=0){
							location.href="/pad_marketing/note/noteIssue.action?url=quotationSign.action&policyRef="+policyRef+"&contractId="+contractId;
							return;
						}else{
							location.href="/pad_marketing/note/affirmNoteReply.action?url=quotationSign.action&policyRef="+policyRef+"&contractId="+contractId;
							return;
						}
					}
			);
				},
				function(data){
					
				},
				true,//是否异步
				true,//是否显示加载电波图
				'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
		);
	});

}